package com.xiaoshuidi.cloud.module.contract.service.approval;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.contract.controller.admin.approval.vo.ContractApprovalConfigExportReqVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.approval.vo.ContractApprovalConfigPageReqVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.approval.vo.ContractApprovalConfigReqVO;
import com.xiaoshuidi.cloud.module.contract.convert.approval.ContractApprovalConfigConvert;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.approval.ContractApprovalConfigDO;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.approval.ContractApprovalConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.xiaoshuidi.cloud.module.contract.enums.ErrorCodeConstants.APPROVAL_CONFIG_NOT_EXISTS;

/**
 * 合同账单审批配置 Service 实现类
 *
 * @author 合房租赁
 */
@Service
public class ApprovalConfigServiceImpl implements ApprovalConfigService {

    @Resource
    private ContractApprovalConfigMapper contractApprovalConfigMapper;

    @Override
    public void createApprovalConfig(ContractApprovalConfigReqVO req) {
        if(req.getId()==null){
            // 插入
            ContractApprovalConfigDO approvalConfig = ContractApprovalConfigConvert.INSTANCE.convert(req);
            contractApprovalConfigMapper.insert(approvalConfig);
        }else {
            updateApprovalConfig(req);
        }
    }

    @Override
    public void updateApprovalConfig(ContractApprovalConfigReqVO updateReqVO) {
        // 校验存在
        validateApprovalConfigExists(updateReqVO.getId());
        // 更新
        ContractApprovalConfigDO updateObj = ContractApprovalConfigConvert.INSTANCE.convert(updateReqVO);
        contractApprovalConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteApprovalConfig(Long id) {
        // 校验存在
        validateApprovalConfigExists(id);
        // 删除
        contractApprovalConfigMapper.deleteById(id);
    }

    private void validateApprovalConfigExists(Long id) {
        if (contractApprovalConfigMapper.selectById(id) == null) {
            throw exception(APPROVAL_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public ContractApprovalConfigDO getApprovalConfig(Long id) {
        return contractApprovalConfigMapper.selectById(id);
    }

    @Override
    public List<ContractApprovalConfigDO> getApprovalConfigList(Collection<Long> ids) {
        return contractApprovalConfigMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ContractApprovalConfigDO> getApprovalConfigPage(ContractApprovalConfigPageReqVO reqVO) {
        LambdaQueryWrapper<ContractApprovalConfigDO> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(ContractApprovalConfigDO::getType,reqVO.getType());
        PageResult<ContractApprovalConfigDO> contractApprovalConfigDOS = contractApprovalConfigMapper.selectPage(reqVO,lambdaQueryWrapper);
        return contractApprovalConfigDOS;
    }

    @Override
    public List<ContractApprovalConfigDO> getApprovalConfigList(ContractApprovalConfigExportReqVO exportReqVO) {
        return null;
    }

}
